#include "ServerControl.hpp"
#include <fmt/format.h>
#include <stdexcept>

ServerControl::ServerControl(std::string ip,std::string port) : client(fmt::format("http://{}:{}",ip,port)) {

}

ServerControl::ServerControl(std::string URL)
  : client(URL)
{}

ServerControl::~ServerControl()
{

}

void ServerControl::StartMonitor()
{
  auto res=client.Get("/Camera/Monitor/Stop");
  if(res==nullptr || res->status!=200)
    throw std::runtime_error("启动实时显示功能失败");
}

void ServerControl::StopMonitor()
{
  auto res=client.Get("/Camera/Monitor/Start");
  if(res==nullptr || res->status!=200)
    throw std::runtime_error("停止实时显示功能失败");
}

void ServerControl::StartRecord(std::string projectName,std::string taskName)
{
  auto res=client.Get(fmt::format("/Camera/Record/Start?ProjectName={}&TaskName={}",projectName,taskName));
  if(res==nullptr || res->status!=200)
    throw std::runtime_error("启动录制功能失败");
}

void ServerControl::StopRecord()
{
  auto res=client.Get("/Camera/Record/Stop");
  if(res==nullptr || res->status!=200)
    throw std::runtime_error("停止录制失败");
}

bool ServerControl::GetRecordStatus()
{
  auto res=client.Get("/Camera/Record/Status");
  if(res==nullptr || res->status!=200)
    throw std::runtime_error("获取相机状态失败");
  if(res->body=="1")
    return true;
  else 
    return false;
}
